package com.garden.minioclient.controller;

import com.garden.minioclient.service.MinioService;
import com.garden.minioclient.vo.FileObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author Garden
 * @date: 2023/10/24 14:51
 */
@RestController
@RequestMapping("/minio")
public class MinioController {

    @Autowired
    private MinioService minioService;

    /**
     * 查看bucket是否存在
     *
     * @param bucketName
     * @return
     */
    @GetMapping("/bucketExists")
    public boolean bucketExists(@RequestParam(name = "bucketName") String bucketName) {
        return minioService.bucketExists(bucketName);
    }

    /**
     * 上传文件
     *
     * @param multipartFile
     * @return
     */
    @PostMapping("/uploadFile")
    public boolean uploadFile(@RequestParam(name = "file") MultipartFile multipartFile) {
        return minioService.uploadFile(multipartFile);
    }

    /**
     * 下载文件
     *
     * @param fileName
     * @param response
     */
    @GetMapping("/downloadFile")
    public void downloadFile(@RequestParam(name = "fileName") String fileName, HttpServletRequest request, HttpServletResponse response) {
        minioService.downloadFile(fileName, request, response);
    }

    /**
     * 删除文件
     *
     * @param fileName
     * @return
     */
    @DeleteMapping("/removeFile")
    public boolean removeFile(@RequestParam(name = "fileName") String fileName) {
        return minioService.removeFile(fileName);
    }

    /**
     * 获取文件Url
     *
     * @param fileName
     * @return
     */
    @GetMapping("/getFileUrl")
    public String getFileUrl(@RequestParam(name = "fileName") String fileName) {
        return minioService.getFileUrl(fileName);
    }

    /**
     * 获取文件对象
     *
     * @param fileName
     * @return
     */
    @GetMapping("/getFileObject")
    public FileObject getFileObject(@RequestParam(name = "fileName") String fileName) {
        return minioService.getFileObject(fileName);
    }

}
